package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Sys_priv;
import model.User_list;

import com.et.ar.exception.ActiveRecordException;
import com.et.mvc.JsonView;
import com.wzzz.util.SysDate;

public class LabourController extends ApplicationController {

	public JsonView getLabours() throws ActiveRecordException{
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		StringBuilder condition = new StringBuilder();
		condition.append("1=1 and USER_COMPANY='").append(company_id).append("'");
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT UL.USER_ID,").append(enter);
		sql.append("       UL.USER_JH,").append(enter);
		sql.append("       UL.USER_PASSWORD,").append(enter);
		sql.append("       UL.USER_NAME,").append(enter);
		sql.append("       nvl(UL.USER_SFZH,'无') USER_SFZH, ").append(enter);
		sql.append("       UL.USER_ZW,").append(enter);
		sql.append("       SP.PRIV_NAME,").append(enter);
		sql.append("       nvl(UL.USER_PHONE,'无') USER_PHONE, ").append(enter);
		sql.append("       UL.USER_COMPANY,").append(enter);
		sql.append("       TCI.COMPANY_NAME,").append(enter);
		sql.append("       nvl(UL.USER_BS,0) user_bs, ").append(enter);
//		sql.append("       UL.USER_LRR,").append(enter);
		sql.append("       nvl(LUL.USER_NAME,'') USER_LRR, ").append(enter);
//		sql.append("       UL.USER_LRR_COMPANY,").append(enter);
		sql.append("       nvl(LRTCI.COMPANY_NAME,'') USER_LRR_COMPANY, ").append(enter);
		sql.append("       UL.USER_TIME").append(enter);
		sql.append("  FROM USER_LIST UL").append(enter);
		sql.append(" INNER JOIN SYS_PRIV SP ON UL.USER_ZW = SP.ID").append(enter);
		sql.append(" INNER JOIN T_COMPANY_INFO TCI ON ul.user_company='").append(company_id).append("' AND UL.USER_COMPANY = TCI.COMPANY_ID").append(enter);
		sql.append("  LEFT JOIN T_COMPANY_INFO LRTCI ON UL.USER_LRR_COMPANY = LRTCI.COMPANY_ID").append(enter);
		sql.append("  LEFT JOIN USER_LIST LUL ON UL.USER_LRR = LUL.USER_ID").append(enter);
//		System.out.println(sql.toString());
		long total = User_list.count(User_list.class,condition.toString(),args);
		List<Map<String,Object>> rows = User_list.getResultMap(User_list.class, sql.toString(), args, "UL.USER_ID", 0, Integer.MAX_VALUE);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);//数据总数
		result.put("rows", rows);
		return new JsonView(result);
	}
	public JsonView getPrivByCompany() throws ActiveRecordException{
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
		String condition = "1=1 and company_id="+company_id;
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		List<Sys_priv> list = Sys_priv.findAll(Sys_priv.class,condition,args);
		return new JsonView(list);
	}
	public JsonView save(User_list ul) throws ActiveRecordException{
		//查看用户名是否存在
		boolean b = checkUserName(ul.getUser_name());
		if(b){
			User_list ul1 = (User_list)session.getAttribute("sessionAccount");
			String company_id = ul1.getUser_company();
			Integer lrr = ul1.getUser_id();
			ul.setUser_company(company_id);
			ul.setUser_lrr(lrr);
			ul.setUser_lrr_company(company_id);
			ul.setUser_time(SysDate.getToday());
			ul.save();
			JsonView view = new JsonView("success:true");
			view.setContentType("text/html;charset=utf-8");
			return view;
		}else{
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("success", false);
			result.put("msg", "用户名已存在");
			JsonView view = new JsonView(result);
			view.setContentType("text/html;charset=utf-8");
			return view;
		}
		
	}
	public JsonView getLabour(int user_id) throws ActiveRecordException{
		User_list ul = User_list.find(User_list.class, user_id);
		return new JsonView(ul);
	}
	public JsonView update(int user_id) throws Exception{
		User_list ul = User_list.find(User_list.class, user_id);
		updateModel(ul);
		ul.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
}
